﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Poco;

namespace Metier
{
    public static class GestionSpecialite
    {
        public static void Ajouter(Specialite specialite)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Specialites.AddObject(specialite);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static Specialite Chercher(int specialiteId)
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Specialites
                        .Where(s => s.Id == specialiteId)
                        .FirstOrDefault();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<Specialite> Lister()
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Specialites.ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<Specialite> Lister(string nom)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from n in db.Specialites
                            where n.Nom.Contains(nom)
                            select n).ToList();

                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void MettreAJour(Specialite specialite)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Specialites.Attach(specialite);
                    db.ObjectStateManager.ChangeObjectState(specialite, System.Data.EntityState.Modified);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void Supprimer(Specialite specialite)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Specialites.DeleteObject(specialite);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
